﻿/*
Copyright (C) 2009  Tomasz Chrzanowski

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections;
using Com.TomCdc.ChangeDataCapture;

namespace CdcCommon.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class CommonInfoTest
    {
        public CommonInfoTest()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {

            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void ConvertToBitArrayTest0()
        {
            BitArray bitA = new BitArray(80);
            for (int i = 0; i < 80; i++)
            {
                bitA[i] = i % 2 == 0 ? false : true; // diff to next test
            }

            string res = CommonHelper.ConvertToHexadecimal(bitA);
            BitArray bitB = CommonHelper.ConvertToBitArray(res);

            for (int i = 0; i < 80; i++)
                Assert.IsTrue(bitA[i] == bitB[i], "Incorrect value at possition " + i);
        }

        [TestMethod]
        public void ConvertToBitArrayTest1()
        {
            BitArray bitA = new BitArray(80);
            for (int i = 0; i < 80; i++)
            {
                bitA[i] = i % 2 == 0 ? true : false; // diff to prev test
            }

            string res = CommonHelper.ConvertToHexadecimal(bitA);
            BitArray bitB = CommonHelper.ConvertToBitArray(res);

            for (int i = 0; i < 80; i++)
                Assert.IsTrue(bitA[i] == bitB[i], "Incorrect value at possition " + i);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ConvertToBitArrayTest2()
        {
            string hex = "FAFAFAFA";
            CommonHelper.ConvertToBitArray(hex);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ConvertToBitArrayTest3()
        {
            string hex = "0xFAFAFAF";
            CommonHelper.ConvertToBitArray(hex);
        }

        [TestMethod]
        public void ConvertToBitArrayTest4()
        {
            string hex = "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
            BitArray res = CommonHelper.ConvertToBitArray(hex);
            foreach (bool b in res) Assert.IsTrue(b);
        }

        [TestMethod]
        public void ConvertToBitArrayTest5()
        {
            string hex = "0x0000000000000000000000000000000000000000";
            BitArray res = CommonHelper.ConvertToBitArray(hex);
            foreach (bool b in res) Assert.IsFalse(b);
        }

        [TestMethod]
        public void ConvertToBitArrayTest6()
        {
            string hex = "0x0F0F";
            BitArray res = CommonHelper.ConvertToBitArray(hex);
            BitArray exp = new BitArray(
                new bool[] { true, true, true, true, false, false, false, false, true, true, true, true, false, false, false, false });
        }

        [TestMethod]
        public void ConvertToBitArrayTest7()
        {
            string hex = "0x0180";
            BitArray res = CommonHelper.ConvertToBitArray(hex);
            BitArray exp = new BitArray(
                new bool[] { true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true });
        }

        [TestMethod]
        public void ConvertToBitArrayTest8()
        {
            string hex = "0x8181";
            BitArray res = CommonHelper.ConvertToBitArray(hex);
            BitArray exp = new BitArray(
                new bool[] { true, false, false, false, false, false, false, true, true, false, false, false, false, false, false, true });
        }

        [TestMethod]
        public void ConvertToBitArrayTest9()
        {
            string hex = "0x00000000000000000000000000000000000000000000000000" +
                "00000000000000000000000000000000000000000000000000" +
                "00000000000000000000000000000000000000000000000000" +
                "00000000000000000000000000000000000000000000000000" +
                "00000000000000000000000000000000000000000000000000";
            BitArray res = CommonHelper.ConvertToBitArray(hex);
            foreach (bool b in res) Assert.IsFalse(b);
        }

        [TestMethod]
        public void ConvertToBitArrayTest10()
        {
            string hex = "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" +
                "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" +
                "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" +
                "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" +
                "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
            BitArray res = CommonHelper.ConvertToBitArray(hex);
            foreach (bool b in res) Assert.IsTrue(b);
        }

        [TestMethod]
        public void ConvertToBitArrayTest11()
        {
            string hex = "0x";
            BitArray res = CommonHelper.ConvertToBitArray(hex);
            Assert.IsTrue(res.Count == 0);
        }
    }
}
